package ar.lalosoft.adMat.parser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import ar.lalosoft.adMat.domain.Materia;

public class MateriaParser {
	

	public List<Materia> getMaterias(String content){
		try{
			/** Handling XML */
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			
			XmlFileHandler xmlBookHandler = new XmlFileHandler();
			xr.setContentHandler(xmlBookHandler);
//			xr.parse(new InputSource(context.getAssets().open(contentUrl)));//content.xml
			xr.parse(new InputSource(content));
			
			//return xmlBookHandler.getMapa();
			return xmlBookHandler.getLista();
		}catch (Exception e) {
		//	Log.e(TAG, e.getMessage());
			}	
		
		return null;
	}
	
	private class XmlFileHandler extends DefaultHandler{
		
		private Map<Integer, Materia> mapa;
		private List<Materia> list;
		private boolean currentElement = false;
		String tag = null;
		String dato;
		
		int codigo;
		int creditos;
		boolean electivas = false;
		ArrayList<Integer> correlativas;
		String nombreMateria;
		
		public XmlFileHandler(){
			super();
			mapa = new HashMap<Integer, Materia>();
			list = new ArrayList<Materia>();
		}
		
		
		@Override
		public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
			
			if(qName.equals("item")){
				currentElement = true;
				correlativas = new ArrayList<Integer>();
				//setear todo...
			}
			
			if(qName.equals("electivas")){
				electivas = true;
			}
			
			if(currentElement){
				tag = attributes.getValue("name");
			}
			
		}
		
		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
			
			if(tag.equals("codigo")){
				codigo = Integer.parseInt(dato);
			}
			
			if(tag.equals("nombre")){
				nombreMateria = dato;
			}
			
			if(tag.equals("creditos")){
				creditos = Integer.parseInt(dato);
			}
			
			if(tag.equals("correlativa")){
				correlativas.add(Integer.parseInt(dato));
				tag = "";
			}

			if(qName.equals("item")){
				Materia materia = null;
				if(electivas){
					materia = new Materia(nombreMateria, codigo, creditos, true);
				} else {
					materia = new Materia(nombreMateria, codigo, creditos);
				}
				
				materia.setCorrelativas(correlativas);
				mapa.put(materia.getCod(), materia);
				list.add(materia);
				currentElement = false;
				electivas = false;
			}
			
		}
		
		/**
		 * Guarda el dato de la cadena de Char --> <localName>DATO</localName>
		 */
		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
			dato = new String(ch, start, length);
		}
		

		public Map<Integer, Materia> getMapa() {
			return mapa;
		}
		
		public List<Materia> getLista(){
			return list;
		}
		
	}

}
